package light.head.controller.oa;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.WorkplanDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Workplan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_MYWORKPLAN)
public class MyWorkplanController extends SupporterController{
	private WorkplanDAO wpDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.MYWORKPLAN_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.TIME1, Parm.DELETED, Parm.TIME2);
		if (wpDAO.findByMap(cu_organCode(request),cu_code(request), map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MYWORKPLAN_LIST);
	}

	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Workplan wp = wpDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.WORKPLAN, wp);
		}
		return new ModelAndView(Url.MYWORKPLAN_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(Workplan wp, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (wp.getName() == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		if (wp.getCode() == null) {
			wp.setOc(cu_organCode(request));
			wp.setCu(cu_code(request));
			wp.setCt(new Date());
			wp.setDeleted(false);
			result = wpDAO.add(wp);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	
	@Autowired
	public void setWpDAO(WorkplanDAO wpDAO) {
		this.wpDAO = wpDAO;
	}

}
